package config

import (
	"gopkg.in/yaml.v2"
	"log"
	"os"
)

var Config *Conf

type Conf struct {
	Jwt    Jwt      `yaml:"jwt"`
	XxlJob XxlJob   `yaml:"xxl-job"`
	Log    Log      `yaml:"log"`
	Db     Database `yaml:"database"`
	Server Server   `yaml:"server"`
	Redis  Redis    `yaml:"redis"`
}
type Server struct {
	Port           string `yaml:"port"`
	RunMode        string `yaml:"runMode"`
	LogLevel       string `yaml:"logLevel"`
	EnabledSwagger bool   `yaml:"enabledSwagger"`
}
type Redis struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	Password string `yaml:"password"`
	DB       int    `yaml:"db"`
}

type Database struct {
	Primary         Datasource `yaml:"primary"`
	Secondary       Datasource `yaml:"secondary"`
	MaxIdleConn     int        `yaml:"max_idle_conn"`
	MaxOpenConn     int        `yaml:"max_open_conn"`
	ConnMaxLifetime int        `yaml:"conn_max_lifetime"`
}

type Log struct {
	Enabled  bool     `yaml:"enabled"`
	LogMode  string   `yaml:"logMode"`
	FilePath string   `yaml:"filePath"`
	Filtered []string `yaml:"filtered"`
}

type XxlJob struct {
	Enabled          bool   `yaml:"enabled"`
	Env              string `yaml:"env"`
	AdminAddress     string `yaml:"admin_address"`
	AccessToken      string `yaml:"access_token"`
	AppName          string `yaml:"app_name"`
	Address          string `yaml:"address"`
	Ip               string `yaml:"ip"`
	Port             int    `yaml:"port"`
	LogPath          string `yaml:"log_path"`
	LogRetentionDays int    `yaml:"log_retention_days"`
	HttpTimeout      int    `yaml:"http_timeout"`
}

type Jwt struct {
	Secret string `yaml:"secret"`
	JwtTtl int    `yaml:"jwt_ttl"`
}
type Datasource struct {
	Type     string `yaml:"type"`
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	UserName string `yaml:"username"`
	Password string `yaml:"password"`
	DbName   string `yaml:"dbname"`
}

func Init() {
	yamlFile, err := os.ReadFile("./config/config.yaml")
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	// 解析YAML文件
	var config Conf
	err = yaml.Unmarshal(yamlFile, &config)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	// 打印解析后的配置
	log.Printf("Config: %+v", config)
	Config = &config

}
